package com.sgb.kjwl.view.ui.activity.self.mine;

import static com.sgb.kjwl.view.ui.fragment.SelfFragment5.SECONDMORE;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.AndroidViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.model.entity.self.SecondMore;
import com.sgb.kjwl.utils.MyLog;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.DialogHelperNew;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SecondmoreModel extends AndroidViewModel {
    private Map<String,Object> map =new HashMap<>();
    public SecondmoreModel(@NonNull Application application) {
        super(application);
    }
    public void GetToplist(SecondMoreActivity activity,int clientType,String siteNo,int siteType ){
        map.put("clientType",clientType);
        map.put("siteNo",siteNo);
        map.put("siteType",siteType);
        APIManager.getInstance().getUserAPI().commonApplicationlist(map).enqueue(new Callback<BaseEntity<SecondMore.DataDTO>>() {
            @Override
            public void onResponse(Call<BaseEntity<SecondMore.DataDTO>> call, Response<BaseEntity<SecondMore.DataDTO>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showchagnyongMenu(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<SecondMore.DataDTO>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    public void getPermissionMenu(CompanyEntity entity,SecondMoreActivity activity) {
        APIManager.getInstance().getUserAPI().getPermissionMenu(entity.getCompNo(), entity.getType()).enqueue(new ICallBack<BaseEntity<List<PermissionMenuEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<PermissionMenuEntity>> data) {
                if (data != null && data.getData() != null) {
                    Gson gson = new Gson();
                    String s = gson.toJson(data.getData(), new TypeToken<List<PermissionMenuEntity>>() {
                    }.getType());
                    List<PermissionMenuEntity> datas = data.getData();
                    List<PermissionMenuEntity> list = new ArrayList<>();
                    if (datas != null && datas.size() != 0) {
                        list.addAll(datas);
                    }
                    activity.showPermissionMenu(list);
                } else {
//                    fragment.dismissRefresh();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionMenuEntity>>> call, Throwable t) {
            }
        });

    }

    public void getNoAuthPermissionMenu(CompanyEntity entity,SecondMoreActivity activity) {
        APIManager.getInstance().getUserAPI().getUnAuthAllMenuPerson(entity.getCompNo(), "0").enqueue(new ICallBack<BaseEntity<List<PermissionMenuEntity>>>(true, true) {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(BaseEntity<List<PermissionMenuEntity>> data) {
                if (data != null && data.getData() != null) {
                    activity.showPermissionMenu(data.getData());
                } else {
//                    fragment.dismissRefresh();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionMenuEntity>>> call, Throwable t) {
            }


        });

    }

    public void updateMenuOrder(SecondMoreActivity activity, Map  map){
        Log.e("李豆",map+"");
        DialogHelperNew.showProgressDialog(activity, "保存中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().saveOrUpDate(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    Intent intent =new Intent();
                    activity.setResult(Activity.RESULT_OK,intent);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 通过标识来获取跳转连接
     *
     * @param map
     */
    public void getStringUrl(SecondMoreActivity activity,Map<String, String> map) {
        String name = map.get("name");
        String type = map.get("type");
        String typeUrl = map.get("typeUrl");
        String param = type;
        final String urlId = typeUrl;
        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                //DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + urlId;
                    WebViewX5HostActivity.start(activity, url);
                }
            }
        });
    }
}
